/**********************************
 * @Author: Your Name
 * @LastEditor: Your Name
 * @LastEditTime: 2025-11-07
 * Copyright © 2025 Your Name
 **********************************/

import { oauthConfigMockData } from './mock'

// 模拟API延迟
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))

// 模拟分页处理
function handlePagination(data, params) {
  const page = params.page || 1
  const pageSize = params.pageSize || 10
  const start = (page - 1) * pageSize
  const end = start + pageSize
  return {
    list: data.slice(start, end),
    total: data.length,
    page,
    pageSize,
  }
}

// 模拟搜索过滤
function handleSearch(data, params) {
  let result = [...data]

  // 根据提供商名称过滤
  if (params.provider) {
    result = result.filter(item => item.provider.toLowerCase().includes(params.provider.toLowerCase()))
  }

  // 根据启用状态过滤
  if (params.enabled !== undefined && params.enabled !== null) {
    result = result.filter(item => item.enabled === params.enabled)
  }

  return result
}

// OAuth配置管理相关API (Mock版本)
export default {
  // 获取OAuth配置列表
  read: async (params = {}) => {
    await delay(500) // 模拟网络延迟

    // 获取所有配置数据
    let configs = [...oauthConfigMockData.configs]

    // 应用搜索过滤
    configs = handleSearch(configs, params)

    // 应用分页
    const pagination = handlePagination(configs, params)

    return Promise.resolve({
      code: 0,
      message: 'success',
      data: {
        list: pagination.list,
        total: pagination.total,
        page: pagination.page,
        pageSize: pagination.pageSize,
      },
    })
  },

  // 创建OAuth配置
  create: async (data) => {
    await delay(500) // 模拟网络延迟

    // 生成新的配置ID
    const newId = Math.max(...oauthConfigMockData.configs.map(c => c.id), 0) + 1

    // 创建新配置对象
    const newConfig = {
      id: newId,
      name: data.name,
      provider: data.provider,
      clientId: data.clientId,
      clientSecret: data.clientSecret,
      redirectUri: data.redirectUri,
      scopes: data.scopes,
      authUrl: data.authUrl,
      tokenUrl: data.tokenUrl,
      userInfoUrl: data.userInfoUrl,
      userInfoMethod: data.userInfoMethod || 'GET',
      icon: data.icon,
      sortOrder: data.sortOrder || 0,
      enabled: data.enabled !== undefined ? data.enabled : true,
      configType: data.configType || 'SYSTEM_BUILTIN',
      customConfig: data.customConfig || null,
      createdAt: new Date().toISOString(),
      updatedAt: new Date().toISOString(),
    }

    // 添加到配置列表
    oauthConfigMockData.configs.push(newConfig)

    return Promise.resolve({
      code: 0,
      message: 'success',
      data: newConfig,
    })
  },

  // 更新OAuth配置
  update: async (data) => {
    await delay(500) // 模拟网络延迟

    // 查找并更新配置
    const index = oauthConfigMockData.configs.findIndex(c => c.id === data.id)
    if (index === -1) {
      return Promise.reject({
        code: 404,
        message: '配置不存在',
      })
    }

    // 更新配置数据
    oauthConfigMockData.configs[index] = {
      ...oauthConfigMockData.configs[index],
      ...data,
      updatedAt: new Date().toISOString(),
    }

    return Promise.resolve({
      code: 0,
      message: 'success',
      data: oauthConfigMockData.configs[index],
    })
  },

  // 删除OAuth配置
  delete: async (id) => {
    await delay(500) // 模拟网络延迟

    // 查找并删除配置
    const index = oauthConfigMockData.configs.findIndex(c => c.id === id)
    if (index === -1) {
      return Promise.reject({
        code: 404,
        message: '配置不存在',
      })
    }

    oauthConfigMockData.configs.splice(index, 1)

    return Promise.resolve({
      code: 0,
      message: 'success',
      data: null,
    })
  },

  // 获取OAuth提供商选项
  getProviderOptions: async () => {
    await delay(300) // 模拟网络延迟

    return Promise.resolve({
      code: 0,
      message: 'success',
      data: oauthConfigMockData.providerOptions,
    })
  },

  // 获取配置类型选项
  getConfigTypeOptions: async () => {
    await delay(300) // 模拟网络延迟

    return Promise.resolve({
      code: 0,
      message: 'success',
      data: oauthConfigMockData.configTypeOptions,
    })
  },

  // 测试OAuth配置连接性
  testConfig: async (id) => {
    await delay(1000) // 模拟测试延迟

    const config = oauthConfigMockData.configs.find(c => c.id === id)
    if (!config) {
      return Promise.reject({
        code: 404,
        message: '配置不存在',
      })
    }

    // 模拟测试结果
    const testResult = {
      success: true,
      message: '连接测试成功',
      configId: id,
      testData: {
        clientId: config.clientId ? '***已配置***' : '未配置',
        clientSecret: config.clientSecret ? '***已配置***' : '未配置',
        authUrl: config.authUrl,
        tokenUrl: config.tokenUrl,
        userInfoUrl: config.userInfoUrl,
      },
    }

    return Promise.resolve({
      code: 0,
      message: 'success',
      data: testResult,
    })
  },
}
